import { comm } from "../../utils/httpRequest";
import Dialog from '@vant/weapp/dialog/dialog';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    selectKey:2,
    modelshow: false,
    name:"",
    dialogParentName:"",
    dialogCurrentItem:{},
    dialogMode:"",
    dialogInputName:"",
    dialogInputOrder:"",
    region:[],
    cardName:"",
    cardId:0,
  },
  onLoad(options) { 
    this.getProductCatalog()
  },
  onShow() { 
    var select_card_info = wx.getStorageSync('select_card_info')
    if(select_card_info){ 
          this.setData({
            cardId:select_card_info.card_id,
            cardName : select_card_info.name+'-'+select_card_info.position
          })
   }
  },
  onSelect(e){
    console.log(e);
    this.setData({
      selectKey :e.detail.item.id
    })
  },
  onTreeAdd(e){
    console.log(e)

    this.setData({
      modelshow:true,
      dialogMode: "add",
      dialogParentName: e.detail.item.name,
      dialogCurrentItem: e.detail.item
    })
  },
  onTreeEdit(e){
    console.log(e)

    this.setData({
      modelshow:true,
      dialogMode: "Edit",
      dialogParentName: e.detail.item.name,
      dialogCurrentItem: e.detail.item,
      dialogInputName:e.detail.item.name,
      dialogInputOrder:e.detail.item.order
    })
  },
  onTreeDel(e){
    console.log("xx")
    Dialog.confirm({
      title: '提示信息',
      message: '确认是否删除？',
    })
      .then(() => {
            //请求接口
            comm.cjRequest({url: "/wxapi/delProductCatalog", data:{id:e.detail.item.id},token:true})
            .then((res) => {
              console.log(res.data.data)
              if(res.data.code == 1){
                wx.showToast({
                  title: '成功',
                  icon:'none'
                })
                this.getProductCatalog()
              }else{
                wx.showToast({
                  title: '失败',
                  icon:'none'
                })
              }
            })
      })
      .catch(() => {
        // on cancel
      });
  },
  goCardHolder(){
    wx.navigateTo({
      url: '/pages/cardholder/index?sourceType=select&sourceUrl=my'
    })
  },
  onDialogColose(){
    this.setData({
      modelshow:false,
    })
  },
  onDialogConfirm(){
    console.log("123",this.data.dialogCurrentItem)
    let data = {}
    if (this.data.dialogMode === "Edit"){
      data = this.data.dialogCurrentItem
      console.log(data)
      data["name"] = this.data.dialogInputName
      data["order"] = this.data.dialogInputOrder
      data["subtype"] = "editProduct"
    }else{
      data["pid"]  = this.data.dialogCurrentItem["id"]
      data["name"] = this.data.dialogInputName
      data["order"] = this.data.dialogInputOrder
      data["card_id"] = this.data.cardId
    }

    //请求接口
    comm.cjRequest({url: "/wxapi/addProductCatalog", data:data,token:true})
    .then((res) => {
      console.log(res.data.data)
      if(res.data.code == 1){
        wx.showToast({
          title: '成功',
          icon:'none'
        })
        this.getProductCatalog()
      }else{
        wx.showToast({
          title: '失败',
          icon:'none'
        })
      }
    })
  },
  getProductCatalog(){
    comm.cjRequest({url: "/wxapi/getProductCatalog", data:{},token:true})
      .then((res) => {
        console.log(res.data.data)
        if(res.data.code == 1){
           this.setData({
            list:res.data.data
           })
        }else{
          this.setData({
            list:[]
          })
        }
    })
  },
  onClickLeft(){
    console.log("xxx")
    wx.navigateBack({
      delta: 2
    })
  }


})